//*************************************************************************************************
//Copyright (c) 2015.year. @hjjer.
//If you are a hjjer(hejianjiao.org), This code is unlicensed, otherwise,is licensed under the GPL.
//*************************************************************************************************

package com.truthbean.ssm.demo.controller;

import com.truthbean.ssm.core.utils.StringUtil;
import com.truthbean.ssm.demo.domain.entity.Enroll;
import com.truthbean.ssm.demo.service.EnrollService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.io.UnsupportedEncodingException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * @author Truthbean
 * @since 2015-12-18 16:00:29
 */
@RequestMapping("/admin/enroll/")
public class EnrollController {
    @Autowired
    private EnrollService enrollService;

    @RequestMapping(value = "manage.html", method = RequestMethod.GET)
    public String manage(Model model) {
        model.addAttribute("enrollMap", enrollService.queryAll());
        model.addAttribute("ConditionQuerying", true);
        model.addAttribute("isConditionActive", true);
        return "/admin/enroll/list";
    }

    private String preChange(Model model, String area, int key) {
        if (area.trim().isEmpty()) {
            model.addAttribute("messae", "请选择地区");
        } else {
            String area$ = null;
            try {
                area$ = new String(area.getBytes("ISO8859-1"), "UTF-8");
            } catch (UnsupportedEncodingException e) {
                Logger.getLogger(EnrollController.class.getName()).log(
                        Level.SEVERE, "编码转换失败", e);
            }
            model.addAttribute("years", enrollService.queryYearByArea(area$));
            model.addAttribute("area", area$);
            model.addAttribute("key", key);
        }
        return "/admin/enroll/update";
    }

    @RequestMapping(value = "post-year-{area}.html", method = RequestMethod.GET)
    public String preAddYearByArea(Model model, String area) {
        return preChange(model, area, 1);
    }

    @RequestMapping(value = "put-year-{area}.html", method = RequestMethod.GET)
    public String preEditYearByArea(Model model, String area) {
        return preChange(model, area, 3);
    }

    @RequestMapping(value = "delete-year-{area}.html", method = RequestMethod.GET)
    public String preRemoveYearByArea(Model model, String area) {
        return preChange(model, area, 2);
    }

    @RequestMapping(value = "delete-area-{area}.html", method = RequestMethod.GET)
    public String preArea(Model model, String area) {
        if (StringUtil.isNotEmpty(area)) {
            String area$ = null;
            try {
                area$ = new String(area.getBytes("ISO-8859-1"), "UTF-8");
            } catch (UnsupportedEncodingException e) {
                Logger.getLogger(EnrollService.class.getName()).log(
                        Level.SEVERE, "编码转换失败", e);
            }
            if (enrollService.deleteEnroll(area$)) {
                model.addAttribute("message", "删除成功");
            } else {
                model.addAttribute("message", "删除失败");
            }
        }
        model.addAttribute("enrollMap", enrollService.queryAll());
        model.addAttribute("ConditionQuerying", true);
        model.addAttribute("isConditionActive", true);
        return "/admin/enroll/list";
    }

    @RequestMapping("/batchDelete")
    public String batchDelete(Model model, String ids) {
        String[] idStr = null;
        if (ids != null) {
            idStr = ids.split(",");
        }
        if ((idStr != null) && (idStr.length > 0)) {
            if (enrollService.batchDeleteEnroll(idStr)) {
                model.addAttribute("message", "删除失败");
            } else {
                model.addAttribute("message", "删除成功");
            }
        }
        model.addAttribute("enrollMap", enrollService.queryAll());
        model.addAttribute("ConditionQuerying", true);
        model.addAttribute("isConditionActive", true);
        return "/admin/enroll/list";
    }

    @RequestMapping("batchAddYear")
    String batchAddYear(Model model, int year, String ids) {
        String[] idStr = null;
        if (ids != null) {
            try {
                ids = new String(ids.getBytes("ISO8859-1"), "UTF-8");
            } catch (UnsupportedEncodingException ex) {
                Logger.getLogger(EnrollController.class.getName()).log(Level.SEVERE, null, ex);
            }
            idStr = ids.split(",");
        }
        if (StringUtil.isNotEmpty(year)) {
            if (enrollService.batchAddYear(idStr, year)) {
                model.addAttribute("message", "添加失败");
            } else {
                model.addAttribute("message", "添加成功");
            }
        } else {
            model.addAttribute("message", "请输入年份");
        }
        model.addAttribute("enrollMap", enrollService.queryAll());
        model.addAttribute("ConditionQuerying", true);
        model.addAttribute("isConditionActive", true);
        return "/admin/enroll/list";
    }

    @RequestMapping(value = "insert", method = RequestMethod.POST)
    public String insert(Model model, String area, int year) {
        if (area.trim().isEmpty()) {
            model.addAttribute("message", "请输入地区");
        } else if (year == 0) {
            model.addAttribute("message", "请输入年份");
        } else if (enrollService.isAreaExist(area)) {
            model.addAttribute("message", "此地区已存在");
        } else {
            Enroll enroll = new Enroll(year, area);
            if (enrollService.save(enroll)) {
                model.addAttribute("message", "添加成功");
            } else {
                model.addAttribute("message", "添加失败");
            }
        }
        model.addAttribute("enrollMap", enrollService.queryAll());
        model.addAttribute("ConditionQuerying", true);
        model.addAttribute("isConditionActive", true);
        return "/admin/enroll/list";
    }

    @RequestMapping(value = "update", method = RequestMethod.POST)
    public String update(Model model, int key, int year, String area, String oldArea,
                         String[] years) {
        if (!oldArea.trim().isEmpty()) {
            switch (key) {
                case 1:
                    if (!enrollService.addYear(oldArea, year)) {
                        model.addAttribute("key", "1");
                        model.addAttribute("area", oldArea);
                        model.addAttribute("years",
                                enrollService.queryYearByArea(oldArea));
                        model.addAttribute("message", "新增年份失败");
                        return "/admin/enroll/update";
                    }
                    break;

                case 2:
                    String oldAreaTmp = null;
                    try {
                        oldAreaTmp = new String(oldArea.getBytes("ISO8859-1"), "UTF-8");
                    } catch (UnsupportedEncodingException e) {
                        Logger.getLogger(EnrollService.class.getName()).log(
                                Level.SEVERE, "编码转换失败", e);
                    }
                    if (!enrollService.updateYear(year, oldAreaTmp, "")) {
                        model.addAttribute("key", "2");
                        model.addAttribute("area", oldArea);
                        model.addAttribute("years", enrollService.queryYearByArea(oldArea));
                        model.addAttribute("message", "删除年份失败");
                        return "/admin/enroll/update";
                    }
                    break;

                case 3:
                    if (years != null && years.length > 0) {
                        int[] oldYears = enrollService.queryYearByArea(oldArea);
                        for (int i = 0; i < years.length; i++) {
                            enrollService.updateYear(oldYears[i], oldArea, years[i]);
                        }
                    }
                    break;

                case 4:
                    if (StringUtil.isNotEmpty(area)) {
                        enrollService.updateArea(area, oldArea);
                    }
                    break;
            }
        }
        model.addAttribute("enrollMap", enrollService.queryAll());
        model.addAttribute("ConditionQuerying", true);
        model.addAttribute("isConditionActive", true);
        return "/admin/enroll/list";
    }
}
